﻿using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Xml;
using System.IO;
using System.Reflection;
using System.Text.Unicode;

namespace Tnelab.MBWrapper
{
    internal static class MBVip
    {
        /// <summary>
        /// 把node.dll展开到程序当前目录
        /// </summary>
        static MBVip()
        {            
            mbInit(IntPtr.Zero);
        }

        [StructLayout(LayoutKind.Sequential)]
        internal struct mbRect
        {
            public int x;
            public int y;
            public int w;
            public int h;
        }
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        internal unsafe delegate void mbPaintBitUpdatedCallback(IntPtr webView, IntPtr param, IntPtr buffer, mbRect* r, int width, int height);
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        [return:MarshalAs(UnmanagedType.Bool)]
        internal delegate bool mbLoadUrlBeginCallback(IntPtr webView, IntPtr param, [MarshalAs(UnmanagedType.LPUTF8Str)]string url, IntPtr job);
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        internal delegate void mbLoadUrlEndCallback(IntPtr webView, IntPtr param, [MarshalAs(UnmanagedType.LPUTF8Str)]string url, IntPtr job, IntPtr buf, int len);
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        internal delegate void mbRunJsCallback(IntPtr webView, IntPtr param, IntPtr es, Int64 v);
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        internal delegate void mbJsQueryCallback(IntPtr webView, IntPtr param, IntPtr es, Int64 queryId, int customMsg, [MarshalAs(UnmanagedType.LPUTF8Str)] string request);


        const string node_dll_name = "mb";
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public static extern void mbInit(IntPtr settings);
        [DllImport(node_dll_name,CallingConvention =CallingConvention.StdCall)]
        public static extern IntPtr mbCreateWebView();
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public static extern void mbSetTransparent(IntPtr mbWebView,[MarshalAs(UnmanagedType.Bool)]bool transparent);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern void mbLoadURL(IntPtr mbWebView, [MarshalAs(UnmanagedType.LPUTF8Str)]string url);
        [DllImport(node_dll_name,CallingConvention =CallingConvention.StdCall)]
        public static extern void mbOnPaintBitUpdated(IntPtr webView, mbPaintBitUpdatedCallback callback, IntPtr callbackParam);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public static extern void mbResize(IntPtr webView, int w, int h);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public static extern void mbOnLoadUrlBegin(IntPtr webView, mbLoadUrlBeginCallback callback, IntPtr callbackParam);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public static extern void mbSetHandle(IntPtr webView, IntPtr wnd);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool mbFireKeyDownEvent(IntPtr webView, uint virtualKeyCode, uint flags, bool systemKey);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool mbFireKeyUpEvent(IntPtr webView, uint virtualKeyCode, uint flags, bool systemKey);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public static extern void mbSetFocus(IntPtr webView);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public static extern void mbKillFocus(IntPtr webView);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool mbFireKeyPressEvent(IntPtr webView, uint virtualKeyCode, uint flags, bool systemKey);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool mbFireMouseWheelEvent(IntPtr webView, int x, int y, int delta, uint flags);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool mbFireMouseEvent(IntPtr webView, uint message, int x, int y, uint flags);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern void mbRunJs(IntPtr webView, IntPtr frameId, [MarshalAs(UnmanagedType.LPUTF8Str)] string script, [MarshalAs(UnmanagedType.Bool)] bool isInClosure, mbRunJsCallback callback, IntPtr param, IntPtr unuse);
        [DllImport(node_dll_name,CharSet =CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr mbJsToString(IntPtr es, Int64 v);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public extern static void mbDestroyWebView(IntPtr webView);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public extern static void mbNetHookRequest(IntPtr jobPtr);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public extern static void mbOnLoadUrlEnd(IntPtr webView, mbLoadUrlEndCallback callback, IntPtr callbackParam);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public extern static void mbNetSetData(IntPtr jobPtr, IntPtr buf, int len);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public extern static void mbOnJsQuery(IntPtr webView, mbJsQueryCallback callback, IntPtr param);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public extern static void mbResponseQuery(IntPtr webView, Int64 queryId, int customMsg, [MarshalAs(UnmanagedType.LPUTF8Str)] string response);
        [DllImport(node_dll_name, CallingConvention = CallingConvention.StdCall)]
        public extern static IntPtr mbWebFrameGetMainFrame(IntPtr webView);
    }
}
